<?php
class ModuleSnsFactory
{

    /**
     * @var ModuleSns
     */
    private static $instance;

    /**
     * Get ModuleSns implementation instance.
     */
    public static function get()
    {
        if (!self::$instance) {
            throw new SzException(30200, null, ModuleSnsConst::MODULE_NAME);
        }
        return self::$instance;
    }

    /**
     * Initialize the platform ModuleSns implementation instance in the factory,
     * and then use ModuleSnsFactory::get() to get the instance.
     * Param $userId is possible to be NULL, when this function called on index.php page,
     * there is only platform id, and no user id.
     *
     * @param int $userId player user id, not platform id
     * @param string $platform platform name, ModuleSns::$PF*
     * @throws SzException 30201
     * @return ModuleSns $instance
     */
    public static function init($userId, $platform)
    {
        $platformOn = SzConfig::get()->loadAppConfig('app', 'PLATFORM_ON');

        if ($platformOn) {
            // platform APIs enabled
            switch ($platform) {
                case ModuleSnsConst::PFFB:
                    self::$instance = self::createFacebookInstance($userId);
                    break;
                case ModuleSnsConst::PFRR:
                    self::$instance = self::createRenrenInstance($userId);
                    break;
                case ModuleSnsConst::PFSA:
                    self::$instance = self::createSinaInstance($userId);
                    break;
                case ModuleSnsConst::PFVK:
                    self::$instance = self::createVkInstance($userId);
                    break;
                case ModuleSnsConst::PFOGZ:
                    self::$instance = self::createOgzInstance($userId);
                    break;
                case ModuleSnsConst::PFTC:
                case ModuleSnsConst::PFQZ:
                case ModuleSnsConst::PFPY:
                    self::$instance = self::createTencentInstance($userId, $platform);
                    break;
                case ModuleSnsConst::PFAG:
                    self::$instance = self::createArmorgameInstance($userId);
                    break;
                case ModuleSnsConst::PFSZ:
                    self::$instance = self::createShinezoneInstance($userId);
                    break;
                case ModuleSnsConst::PFNO:
                    self::$instance = new ModuleSnsNo(); // do not initialize self instance, just return no platform specified instance
                    break;
                default:
                    throw new SzException(30201, $platform, ModuleSnsConst::MODULE_NAME); // platform name invalid
                    break;
            }
        } else {
            // platform APIs disabled
            self::$instance = new ModuleSnsNo();
        }
        return self::$instance;
    }

    /**
     * Load facebook config file, create ModuleSnsSina instance.
     *
     * @param int $userId
     * @return ModuleSnsFacebook $instance
     */
    private static function createFacebookInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFFB), null, true);
        return new ModuleSnsFacebook($userId, ModuleSnsConst::PFFB, $configs);
    }

    /**
     * Load renren config file, create ModuleSnsRenren instance.
     *
     * @param int $userId
     * @return ModuleSnsRenren $instance
     */
    private static function createRenrenInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFRR), null, true);
        return new ModuleSnsRenren($userId, ModuleSnsConst::PFRR, $configs);
    }

    /**
     * Load tencent config file, create ModuleSnsTencent instance.
     *
     * @param int $userId
     * @param string $platform
     * @return ModuleSnsTencent $instance
     */
    private static function createTencentInstance($userId, $platform)
    {
        $configs = SzConfig::get()->loadAppConfig(
            ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFTC), null, true
        ); // all tencent sub platforms using config file 'tencent'
        return new ModuleSnsTencent($userId, $platform, $configs);
    }

    /**
     * Load sina config file, create ModuleSnsSina instance.
     *
     * @param int $userId
     * @return ModuleSnsSina $instance
     */
    private static function createSinaInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFSA), null, true);
        return new ModuleSnsSina($userId, ModuleSnsConst::PFSA, $configs);
    }

    /**
     * Load vk config file, create ModuleSnsVk instance.
     *
     * @param int $userId
     * @return ModuleSnsVk $instance
     */
    private static function createVkInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFVK), null, true);
        return new ModuleSnsVk($userId, ModuleSnsConst::PFVK, $configs);
    }

    /**
     * Load armorgame config file, create ModuleSnsVk instance.
     *
     * @param int $userId
     * @return ModuleSnsArmorgame $instance
     */
    private static function createArmorgameInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFAG), null, true);
        return new ModuleSnsArmorgame($userId, ModuleSnsConst::PFAG, $configs);
    }

    /**
     * Load OGZ config file, create ModuleSnsOGZ instance.
     *
     * @param $userId
     * @return ModuleSnsOGZ $instance
     */
    private static function createOgzInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFOGZ), null, true);
        return new ModuleSnsOgz($userId, ModuleSnsConst::PFOGZ, $configs);
    }

    /**
     * Load Shinezone config file, create ModuleSnsShinezone instance.
     *
     * @param $userId
     * @return ModuleSnsShinezone $instance
     */
    private static function createShinezoneInstance($userId)
    {
        $configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(ModuleSnsConst::PFSZ), null, true);
        return new ModuleSnsShinezone($userId, ModuleSnsConst::PFSZ, $configs);
    }
}